/**
 * 页面加载初始化
 */
$(function(){
	var inputType = $("#output_Type option:selected");
	selectOutputType(inputType[0])
});
/**
 * 输入配置点击添加事件弹出输入框界面;
 */
$("#output_config").click(function() {
	selectOutputType($("#output_Type option:selected").val("console")[0]);
	$("#commonDiv").html($("#outputTypeContainer").html());
	bootbox.dialog({
		title : "输出规则配置添加",
		onEscape : true,
		message :$("#inputDomain").html() ,
		size: 'large',
		buttons : {
			button : {
				label : "添加",
				callback : function() {
					var inputType = $("#output_Type option:selected");
					setOutputTextArea(inputType.val());
					return true;
				}
			}
		}
	});
});
/**
 * 设置输入配置信息;
 * @param inputType
 */
function setOutputTextArea(inputType)
{
	var value = output.getValue();
	if(inputType == "console")//设置集合UrlStr
	{
		value = outputConsoleToJson(value);
	}else if(inputType == "file")//设置文件JsonStr;
	{
		value = outputFileToJson(value);
	}else if(inputType == "jdbc")//设置JdbcJsonStr;
	{
		value = outputJdbcToJson(value);
	}else if(inputType == "mongo")//设置DbJsonStr;
	{
		value = outputDbToJson(value);
	}
	output.setValue(value);
}
/**
 * 输出Console转为JsonStr
 */
function outputConsoleToJson(inputAreaValue)
{
	var inputListJsonStr = "#console\n{}";
	if(inputAreaValue != "" && inputAreaValue != null){
		inputAreaValue +="\n";
	}
	inputAreaValue += inputListJsonStr;
	return inputAreaValue;
}
/**
 * 输入文件配置信息转为JsonStr
 * @param inputAreaValue
 * @returns
 */
function outputFileToJson(inputAreaValue)
{
	var path = $(".bootbox-body input[name='path']").val();
	var charEncoding = $(".bootbox-body input[name='charEncoding']").val();
	var max = $(".bootbox-body input[name='max']").val();
	var append = $(".bootbox-body input:radio:checked").val();
	var inputFileJsonStr = "#file\n{\n";
	if(path)
	{
		inputFileJsonStr +=" path:'"+path+"',\n";
	}
	if(charEncoding)
	{
		inputFileJsonStr +=" charEncoding:'"+charEncoding+"',\n";
	}
	if(max)
	{
		inputFileJsonStr +=" max:"+max+",\n";
	}
	if(append)
	{
		inputFileJsonStr += " append:"+append+"\n";
	}
	inputFileJsonStr += "}";
	if(inputAreaValue != "" && inputAreaValue != null){
		inputAreaValue +="\n";
	}
	inputAreaValue += inputFileJsonStr;
	return inputAreaValue;
}
/**
 * 输入jdbc信息转为JsonStr
 * @param inputAreaValue
 * @returns
 */
function outputJdbcToJson(inputAreaValue)
{
	var jdbcUrl = $(".bootbox-body input[name='jdbcUrl']").val();
	var username = $(".bootbox-body input[name='username']").val();
	var password = $(".bootbox-body input[name='password']").val();
	var batchSize = $(".bootbox-body input[name='batchSize']").val();
	var tableName = $(".bootbox-body div.container-output-jdbc input[name='tableName']").val();
	var inputJdbcJsonStr = "#jdbc\n{\n";
	if(jdbcUrl)
	{
		inputJdbcJsonStr +=" jdbcUrl:'"+jdbcUrl+"',\n";
	}
	if(username)
	{
		inputJdbcJsonStr +=" username:'"+username+"',\n";
	}
	if(password)
	{
		inputJdbcJsonStr +=" password:'"+password+"',\n";
	}
	if(tableName)
	{
		inputJdbcJsonStr +=" tableName:'"+tableName+"',\n";
	}
	if(batchSize)
	{
		inputJdbcJsonStr +=" batchSize:"+batchSize+"\n";
	}
	if(inputJdbcJsonStr.length == inputJdbcJsonStr.lastIndexOf(",")+2)
	{
		inputJdbcJsonStr = inputJdbcJsonStr.substring(0,inputJdbcJsonStr.lastIndexOf(","))+"\n";
	}
	inputJdbcJsonStr += "}";
	if(inputAreaValue != "" && inputAreaValue != null){
		inputAreaValue +="\n";
	}
	inputAreaValue += inputJdbcJsonStr;
	return inputAreaValue;
}
/**
 * 输入数据库信息转为JsonStr
 * @param inputAreaValue
 * @returns
 */
function outputDbToJson(inputAreaValue)
{
	var host = $(".bootbox-body input[name='host']").val();
	var port = $(".bootbox-body input[name='port']").val();
	var dbName = $(".bootbox-body input[name='dbName']").val();
	var tableName = $(".bootbox-body div.container-output-db input[name='tableName']").val();
	var batchSize = $(".bootbox-body div.container-output-db input[name='batchSize']").val();
	var inputDbJsonStr = "#mongo\n{\n";
	if(host)
	{
		inputDbJsonStr +=" host:'"+host+"',\n";
	}
	if(port)
	{
		inputDbJsonStr +=" port:"+port+",\n";
	}
	if(dbName)
	{
		inputDbJsonStr +=" dbName:'"+dbName+"',\n";
	}
	if(tableName)
	{
		inputDbJsonStr +=" tableName:'"+tableName+"',\n";
	}
	if(batchSize)
	{
		inputDbJsonStr +=" batchSize:"+batchSize+"\n";
	}
	if(inputDbJsonStr.length == inputDbJsonStr.lastIndexOf(",")+2)
	{
		inputDbJsonStr = inputDbJsonStr.substring(0,inputDbJsonStr.lastIndexOf(","))+"\n";
	}
	inputDbJsonStr += "}";
	if(inputAreaValue != "" && inputAreaValue != null){
		inputAreaValue +="\n";
	}
	inputAreaValue += inputDbJsonStr;
	return inputAreaValue;
}
/**
 * 选择下拉框选择列表;
 * @param obj
 */
function selectOutputType(obj)
{
	var value = $(obj).val();
	$("#output_Type option:selected").val(value);
	if("console" == value)
	{
		$("div.container-output-file").hide();
		$("div.container-output-jdbc").hide();
		$("div.container-output-db").hide();
	}else if("file" == value)
	{
		$("div.container-output-jdbc").hide();
		$("div.container-output-file").show();
		$("div.container-output-db").hide();
	}else if("jdbc" == value){
		$("div.container-output-jdbc").show();
		$("div.container-output-file").hide();
		$("div.container-output-db").hide();
	}else if("mongo" == value)
	{
		$("div.container-output-jdbc").hide();
		$("div.container-output-file").hide();
		$("div.container-output-db").show();
	}
}